﻿<?php
include_once('config.inc.php');
if(!isset($path))
{
$path=""; //URL rewriting makes file look into folders but that creates an issue while importing file cause Apache still believes it's similar to a real folder.
}

// SPECIAL OP: making the user staying connected using cookies
if(isset($_POST['op']) AND $_POST['op']=="manage")
{	session_start();
    $_SESSION['id'] = $_POST['user_id'];
    $_SESSION['email'] = $_POST['user_email'];
	setcookie("id", $_POST['user_id'], time() + (86400 * 30), "/");
	setcookie("pwd", $_POST['user_hash_pwd'], time() + (86400 * 30), "/");
	//Instant redirect after the cookies are created
	echo'<script language=javascript>
	document.location = "'.$path.'account/";
	</script>';
}
$cookies_bool = isset($_COOKIE['id']) AND isset($_COOKIE['pwd']);
if(isset($_GET['op']) AND $_GET['op']=="logout")
{	
	setcookie("id", "", time() - 3600, "/");
	setcookie("pwd", "", time() - 3600, "/");
	echo'<script language=javascript>
	document.location = "'.$path.'?op=null";
	</script>';
}

// User connexion status
// (Checking if the cookies contains right email/hashpwd combination.
// Indeed, you can change the cookie content from the browser so we have to make sure the user
// didn't just change the user id stored in them before using it.)
if($cookies_bool)
		{
		$req = $db->prepare('SELECT email FROM jktrade_users WHERE id = :id AND pwd = :password');
		$req->execute(array(
			'id' => $_COOKIE['id'],
			'password' => $_COOKIE['pwd']));
		$user_connected = $req->fetch(); // true if it matches, false if a hacker tries to dump the server
		}
else {$user_connected=false;}


?>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

	<meta charset="utf-8">

	<title>Jönkötrade</title>
	<meta name="description" content="Jönköping Trading Website">
	<meta name="author" content="TheJals.com">

	<!-- Mobile Metas -->
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<!-- CSS -->
	<link rel="stylesheet" href="<?php echo $path; ?>data/css/font-awesome.css">
	<link rel="stylesheet" href="<?php echo $path; ?>data/css/animate.css">
	<link rel="stylesheet" href="<?php echo $path; ?>data/css/flexslider.css">
	<link rel="stylesheet" href="<?php echo $path; ?>data/css/skeleton.css">
	<link rel="stylesheet" href="<?php echo $path; ?>data/css/main.css">

	<!-- Fonts -->
	<link href="<?php echo $path; ?>data/css/css.css" rel="stylesheet" type="text/css">
	<link href="<?php echo $path; ?>data/css/css_003.css" rel="stylesheet" type="text/css">
	<link href="<?php echo $path; ?>data/css/css_002.css" rel="stylesheet" type="text/css">
	<link href="<?php echo $path; ?>data/css/css_004.css" rel="stylesheet" type="text/css">
	<link rel="shortcut icon" href="<?php echo $path; ?>data/img/favicon.ico">
</head>

<body class="landing-page">
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '708981755860452',
      xfbml      : true,
      version    : 'v2.1'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

	<header class="header" id="header">
		<div class="container">
			
			<div class="sixteen columns">

				<div class="logo">
					<a href="<?php echo $path; ?>index.php"><img src="<?php echo $path; ?>data/img/logo.png" alt="Jonkotrade Logo"></a>
				</div>
			
			
				<nav class="nav">
					<!-- Mobile Menu Toggle -->
					<a href="#" id="menu-toggle" class="menu-toggle">Menu <i class="fa fa-align-justify"></i></a>

					<ul id="menu" class="menu">
					<?php echo '
						<li '.(($page == 'home')? ' class="active" ' : '').'><a href="'.$path.'">Home</a></li>
						<li><a href="'.$path.'products/">Products</a></li>
						<li><a href="'.$path.'about/">About us</a></li>
						';
						
						if($user_connected) // "Login or Register Link" ==> "Hi, (First Name)", "Cart" and "Logout"
						{
						$reponse = $db->query('SELECT firstname FROM jktrade_users WHERE id='.$_COOKIE['id'].'');
						$data = $reponse->fetch();
							echo'<li '.(($page == 'account')? ' class="active" ' : '').'><a href="'.$path.'account/">Logged as '.$data['firstname'].' </a></li>
							<li><a href="'.$path.'cart/"><i class="fa fa-shopping-cart"></i> Cart</a></li>
							<li><a href="'.$path.'?op=logout">Log out</a></li>';
						}
						else
						{
							echo'<li '.(($page == 'account')? ' class="active" ' : '').'><a href="'.$path.'account/">Log in or Register</a></li>
							<li><a href="'.$path.'cart/"><i class="fa fa-shopping-cart"></i> Cart</a></li>
							';
						}
						?>
					</ul>
				</nav>
			
			</div> <!-- /.sixteen columns -->
		
		</div><!-- /.container -->
	</header>

